package com.orangelife.service.propFeeMeterType.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeMeterType.IPropFeeMeterTypeService;
import com.orangelife.service.propFeeMeterType.model.PropFeeMeterTypeDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeMeterType类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-07-04 11:03:58
 * ==============================================================
 */
@Service("propFeeMeterTypeService")
public class PropFeeMeterTypeServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeMeterTypeService {
	private static Logger logger = Logger
			.getLogger(PropFeeMeterTypeServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeMeterTypeServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	
	public int addPropFeeMeterType(PropFeeMeterTypeDto propFeeMeterType)
			throws AppBizException {

		Assert.notNull(propFeeMeterType);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeMeterType.getMeterTypeName())) {
			if (propFeeMeterType.getMeterTypeName().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterType.getDescription())) {
			if (propFeeMeterType.getDescription().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "描述长度超出");
			}
		}
		List<PropFeeMeterTypeDto> propFeeMeterTypeDtos = this
				.findPropFeeMeterTypeByRelevantID(null,
						propFeeMeterType.getCommID(),
						propFeeMeterType.getMeterTypeName());

		if (propFeeMeterTypeDtos != null && propFeeMeterTypeDtos.size() > 0) {
			throw new AppBizException(2, "亲，该表类型已经存在");
		}

		propFeeMeterType.setMeterTypeID(IdUtil.getuuId());
		propFeeMeterType.setItemStat(TypeConst.FLAG_ON);
		propFeeMeterType.setTscreate(new Date());
		propFeeMeterType.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeMeterTypeMapper.insertSelective", propFeeMeterType);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	
	public int editPropFeeMeterType(PropFeeMeterTypeDto propFeeMeterType)
			throws AppBizException {

		Assert.notNull(propFeeMeterType);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeMeterType.getMeterTypeID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeMeterTypeDto oldPropFeeMeterType = this
				.findPropFeeMeterTypeByID(propFeeMeterType.getMeterTypeID());

		if (!StringUtils.isBlank(propFeeMeterType.getMeterTypeName())) {
			if (propFeeMeterType.getMeterTypeName().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterType.getDescription())) {
			if (propFeeMeterType.getDescription().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "描述长度超出");
			}
		}
		List<PropFeeMeterTypeDto> propFeeMeterTypeDtos = this
				.findPropFeeMeterTypeByRelevantID(null,
						propFeeMeterType.getCommID(),
						propFeeMeterType.getMeterTypeName());

		if (propFeeMeterTypeDtos != null && propFeeMeterTypeDtos.size() > 1) {
			throw new AppBizException(2, "亲，该表类型已经存在");
		}

		if (propFeeMeterTypeDtos != null && propFeeMeterTypeDtos.size() == 1) {
			if (!propFeeMeterTypeDtos.get(0).getMeterTypeID()
					.equals(propFeeMeterType.getMeterTypeID())) {
				throw new AppBizException(2, "亲，该表类型已经存在");
			}
		}
		if (oldPropFeeMeterType == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeMeterType.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeMeterTypeMapper.updateByPrimaryKeySelective",
				propFeeMeterType);
	}

	@Override
	public PropFeeMeterTypeDto findPropFeeMeterTypeByID(
			String propFeeMeterTypeID) throws AppBizException {
		if (StringUtils.isBlank(propFeeMeterTypeID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeMeterTypeDto oldPropFeeMeterType = this.getSqlSession()
				.selectOne("PropFeeMeterTypeSearchMapper.selectByPrimaryKey",
						propFeeMeterTypeID);
		if (oldPropFeeMeterType == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeMeterType.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeMeterType;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	
	public int deletePropFeeMeterTypeByLogic(String propFeeMeterTypeID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeMeterTypeID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeMeterTypeDto oldPropFeeMeterType = this
				.findPropFeeMeterTypeByID(propFeeMeterTypeID);

		oldPropFeeMeterType.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeMeterType.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeMeterTypeMapper.updateByPrimaryKeySelective",
				oldPropFeeMeterType);
	}

	@Override
	
	public List<Map<String, Object>> findPropFeeMeterTypeByPage(String commID,
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		map.put("commID", commID);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeMeterTypeSearchMapper.findAllPropFeeMeterType", map);
		/*
		 * if (list != null && list.size() > 0) { List<String> commIDs = new
		 * ArrayList<String>(); for (Map<String, Object> propFeeMeter : list) {
		 * commIDs.add(propFeeMeter.get("comm_id").toString()); }
		 * List<Map<String, Object>> comms = com.orangelife.utils.JsonMapUtils
		 * .getPageEntityMap(HttpSvcUtils.doPostSvc(UrlPath.bauthComm, commIDs,
		 * null));
		 * 
		 * Map<String, String> connValue = new HashMap<String, String>();
		 * 
		 * connValue.put("comm", "comm"); list = SplicingUtils.connoList(list,
		 * comms, "comm_id", "commID", connValue);
		 * 
		 * }
		 */
		return list;
	}

	@Override
	public List<PropFeeMeterTypeDto> getPropFeeMeterTypeByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeMeterTypeDto> list = this.getSqlSession().selectList(
				"PropFeeMeterTypeSearchMapper.getAllPropFeeMeterTypeDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeMeterTypeByRelevantID(
			String meterTypeID, String commID, String meterTypeName)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("meterTypeID", meterTypeID);
		map.put("commID", commID);
		map.put("meterTypeName", meterTypeName);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterTypeSearchMapper.selectPropFeeMeterTypeDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeMeterTypeDto> findPropFeeMeterTypeByRelevantID(
			String meterTypeID, String commID, String meterTypeName)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("meterTypeID", meterTypeID);
		map.put("commID", commID);
		map.put("meterTypeName", meterTypeName);

		List<PropFeeMeterTypeDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterTypeSearchMapper.findPropFeeMeterTypeDtoByRelevantID",
						map);
		return list;
	}

	@Override
	
	public List<PropFeeMeterTypeDto> bauthfindPropFeeMeterType(
			List<String> propFeeMeterTypeIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeMeterTypeDto> list = this.getSqlSession().selectList(
				"PropFeeMeterTypeSearchMapper.batchFindPropFeeMeterType",
				propFeeMeterTypeIDs);
		return list;
	}

	@Override

	public List<Map<String, Object>> bauthfindPropFeeMeterTypeByMap(
			Map<String, Object> propFeeMeterType) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeMeterTypeSearchMapper.batchFindPropFeeMeterTypeByMap",
				propFeeMeterType);
		return list;
	}

}